
public class SexprPrinter {

	/**
	 * @param args
	 */
	//Sexpr sexpr;
	boolean needSpace;
/*	public SexprPrinter(Sexpr sexpr)
	{
		this.sexpr = sexpr;
	}*/
	public void atomPrinter(Sexpr sexpr) throws Exception
	{
		if(sexpr.isAtom == true)
		{
			System.out.print(sexpr.Value);
		}
		else
			System.err.println("The S-expr is not an atom "+ sexpr.Value);
		
	}
	public int sexprPrinter(Sexpr sexpr) throws Exception
	{
		needSpace = false;
		if(sexpr.isAtom == true)
		{
			this.atomPrinter(sexpr);
		}
		else
		{
			System.out.print("(");
			do
			{
				if(needSpace == true)
				{
					System.out.print(" ");
				}
				else
					needSpace = true;
				if(sexpr.car != null)
				{
					if(sexpr.car.isAtom == true)
					{
						//sexpr = sexpr.car;
						this.atomPrinter(sexpr.car);
					}
					else
					{
						this.sexprPrinter(sexpr.car);
					}			//	sexpr = sexpr.cdr;

				}
				if(sexpr.cdr != null)
				{
					
					if(sexpr.cdr.isAtom == true)
					{
						if(sexpr.cdr.Value.equals("NIL") == true)
						{
							//sexpr =sexpr.cdr;
							//this.atomPrinter(sexpr.cdr);
							System.out.print(")");
							return 0;
						}					
					}
					if(sexpr.cdr.isAtom == true)
					{
						System.out.print(".");
						this.atomPrinter(sexpr.cdr);
						System.out.println(")");
						return 0;
					}
				}	
				sexpr = sexpr.cdr;					
			}while(true);	
		}
		//System.out.print(")");
		return 0;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
